package oocl.eshop.ui.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;

import oocl.eshop.po.Food;
import oocl.eshop.po.Merchant;
import oocl.eshop.service.FoodManager;
import oocl.eshop.service.impl.FoodManagerImpl;
import oocl.eshop.util.regular.DataMatching;

/**
 * Servlet Filter implementation class FoodEleFilter
 */
public class FoodEleFilter implements Filter {
	FoodManager foodManager = new FoodManagerImpl();

	/**
	 * Default constructor.
	 */
	public FoodEleFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request2 = (HttpServletRequest) request;
		HttpSession session = request2.getSession();
		Merchant merchant = (Merchant) session.getAttribute("merchant");
		// fileupload的参数设置对象
		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(1024 * 300);
		// fileupload的工作核心对象
		ServletFileUpload fileUpload = new ServletFileUpload(factory);
		// fileItem封装各个表单项，每个fileitem是一个input
		try {
			List<FileItem> fileItems = fileUpload.parseRequest(new ServletRequestContext(request2));
			Food food = foodManager.LoadFileFood(fileItems,merchant);
			System.out.println(food);
			Food food2 = foodManager.findFoodByid(food.getfId());
			if (food2 != null) {
				if (food.getfName() != null)
					food2.setfName(food.getfName());
				if (food.getfPrice() != 0)
					food2.setfPrice(food.getfPrice());
				if (food.getfViewPath() != null)
					food2.setfViewPath(food.getfViewPath());
				food=food2;
			}
			// 匹配菜品格式
			if (!DataMatching.isName(food.getfName())) {
				request.setAttribute("Error", "Food name should be letter between 3 to 20.");
			}
			// 匹配菜品价钱
			if (!DataMatching.isPrice(food.getfPrice() + "")) {
				request.setAttribute("Error", "Price should be positive number.");
			}
			if (request.getAttribute("Error") == null) {
				request.setAttribute("thisFood", food);
				
				chain.doFilter(request, response);
			} else {
				request.getRequestDispatcher("menuPage.jsp").forward(request, response);
			}
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
